export default class LoadManager {
    constructor()
    {

    }
    private static _instance:LoadManager;
    
    public static get instance() : LoadManager {
        if(!this._instance)
        {
            this._instance = new LoadManager()
        }
        return this._instance
    }
    private fontResList:Array<cc.SpriteFrame> = []
    private _isLoadedFont:boolean = false
    private preloadFont() {
        cc.loader.loadResDir("font/",cc.Font,function(error,resList:Array<cc.SpriteFrame>,urlList:Array<string>) {
            if(error)
            {
                console.log(error);
                return
            }
            for (let index = 0; index < resList.length; index++) {
                if (resList[index] instanceof cc.Font)
                {
                    const url:string = urlList[index]
                    this.fontResList[url] = resList[index]
                }
            }
            this._isLoadedFont = true
        }.bind(this))
    }

    private imgResList:Array<cc.SpriteFrame> = []
    public getImgResListByUrl(url:string) {
        return this.imgResList[url]
    }
    private _isLoadedImage:boolean = false
    /**
     * 预加载图片资源
     */
    private preloadImage() {
        cc.loader.loadResDir("image/",cc.SpriteFrame,function(error,resList:Array<cc.SpriteFrame>,urlList:Array<string>) {
            if(error)
            {
                console.log(error);
                return
            }
            for (let index = 0; index < resList.length; index++) {
                if (resList[index] instanceof cc.SpriteFrame)
                {
                    const url:string = urlList[index]
                    this.imgResList[url] = resList[index]
                }
            }
            this._isLoadedImage = true
        }.bind(this))
    }
    private skeletonDataList:Array<cc.Prefab> = []
    /**
     * get
     */
    public getSkeletonDataByUrl(url:string) {
        return this.skeletonDataList[url]
    }
    private _isLoadedSpine:boolean = false
    
    /**
     * 预加载spine
     */
    private preloadSpine() {
        cc.loader.loadResDir("spine/",sp.SkeletonData,function(error,resList:Array<sp.SkeletonData>,urlList:Array<string>) {
            if(error)
            {
                console.log(error);
                return
            }
            for (let index = 0; index < resList.length; index++) {
                if (resList[index] instanceof sp.SkeletonData)
                {
                    const url:string = urlList[index]
                    this.skeletonDataList[url] = resList[index]
                }
            }
            this._isLoadedSpine = true
        }.bind(this))
    }
    private prefabResList:Array<cc.Prefab> = []
    /**
     * 获取预制体资源
     */
    public getPrefabResByUrl(url:string) {
        return this.prefabResList[url]
    }
    
    private _isLoadedPrefab:boolean = false
    /**
     * 预加载预制体
     */
    private preloadPrefab() {
        cc.loader.loadResDir("prefab/",cc.Prefab,function(error,resList:Array<cc.Prefab>,urlList:Array<string>) {
            if(error)
            {
                console.log(error);
                return
            }
            for (let index = 0; index < resList.length; index++) {
                if (resList[index] instanceof cc.Prefab)
                {
                    const url:string = urlList[index]
                    this.prefabResList[url] = resList[index]
                }
            }
            this._isLoadedPrefab = true
        }.bind(this))
    }

    private staticConfigResList:Array<object> = []
    /**
     * 获取静态配置表资源
     */
    public getStaticConfigResByUrl(url:string) {
        return this.staticConfigResList[url]
    }
    
    private _isLoadedStaticConfig:boolean = false
    /**
     * preloadStaticConfig
     */
    private preloadStaticConfig() {
        cc.loader.loadResDir("config/",cc.JsonAsset,function(error,resList:Array<cc.JsonAsset>,urlList:Array<string>) {
            if(error)
            {
                console.log(error);
                return
            }
            for (let index = 0; index < resList.length; index++) {
                if (resList[index] instanceof cc.JsonAsset)
                {
                    const url:string = urlList[index]
                    let jsonAsset:cc.JsonAsset = resList[index]
                    this.staticConfigResList[url] = jsonAsset.json
                }
            }
            this._isLoadedStaticConfig = true
        }.bind(this))
    }
    public get isLoadedResComplete() : boolean {
        if(!this._isLoadedFont)
        {
            return false
        }
        if(!this._isLoadedImage)
        {
            return false
        }
        if(!this._isLoadedSpine)
        {
            return false
        }
        if(!this._isLoadedPrefab)
        {
            return false
        }
        if(!this._isLoadedStaticConfig)
        {
            return false
        }
        return true
    }
    /**
     * init
     */
    public init() {
        this.preloadFont()
        this.preloadImage()
        this.preloadSpine()
        this.preloadPrefab()
        this.preloadStaticConfig()

    }
}
